package cc.rengu.igas.bomp.common.dao.impl;

import cc.rengu.igas.bomp.common.dao.AreaInfoMapper;
import cc.rengu.igas.bomp.common.entity.AreaInfo;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

public class AreaInfoMapperImpl implements AreaInfoMapper {
    RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    @Override
    public List<AreaInfo> selectAreaInfoByAreaCodeOrAreaName(String areaCode, String areaName) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectAreaInfoByAreaCodeOrAreaName");
        AreaInfo areaInfo = new AreaInfo();
        areaInfo.setAreaCode(areaCode);
        areaInfo.setAreaName(areaName);

        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, areaInfo, AreaInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据地区代码<{}>、地区名称<{}>获取地区列表失败!", areaCode, areaName);
            return null;
        }
        return objectList.stream().map(item -> (AreaInfo) item).collect(Collectors.toList());
    }

    @Override
    public PageResult selectAreaInfoPageByKeys(String areaCode, String areaName, PageInfo pageInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectAreaInfoByAreaCodeOrAreaName");

        AreaInfo areaInfo = new AreaInfo();
        areaInfo.setAreaCode(areaCode);
        areaInfo.setAreaName(areaName);

        PageResult pageResult = dbsUtil.dbsFetchPageListData(sqlStr, areaInfo, AreaInfo.class, pageInfo);
        if (null == pageResult) {
            rglog.error("根据地区代码<{}>、地区名称<{}>获取地区列表失败!", areaCode, areaName);
            return null;
        }
        return pageResult;
    }
}
